package controllers

import (
	"CRMSystem/models"
	"encoding/json"
)

type ServiceController struct {
	MainController
}

// GetCustomerInformation 获得客户信息
func (c *ServiceController) GetCustomerInformation() {
	c.IfHasSession()
	id, _ := c.GetInt("customerID")
	customer, err := models.GetCustomerViewInformationByCustomerID(id)
	if err != nil {
		c.Data["json"] = Status{ErrCode: GET_ERR, Msg: "Error"}
		c.ServeJSON()
		c.StopRun()
	} else {
		c.Data["json"] = Status{Date: customer, ErrCode: GET_OK, Msg: "GetServiceInfoList:success"}
		c.ServeJSON()
		c.StopRun()
	}
}

// GetCustomerServiceInfoList 获得客户服务表
func (c *ServiceController) GetCustomerServiceInfoList() {
	c.IfHasSession()
	id, _ := c.GetInt("customerID")
	services, i := models.GetServiceListInformationByCustomerID(id)
	if i > 0 {
		c.Data["json"] = Status{Date: services, ErrCode: GET_OK, Msg: "GetCustomerServiceInfoList success"}
		c.ServeJSON()
		c.StopRun()
	} else {
		c.Data["json"] = Status{ErrCode: GET_ERR, Msg: "Error"}
		c.ServeJSON()
		c.StopRun()
	}
}

// AddService 添加服务
func (c *ServiceController) AddService() {
	c.IfHasSession()
	sub := models.ServiceSubmitModel{}
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &sub); err == nil {
		if sub.ServiceName == "" {
			c.Data["json"] = Status{ErrCode: POST_ERR, Msg: "Error"}
			c.ServeJSON()
			c.StopRun()
		}
		err = models.AddService(sub)
		if err != nil {
			c.Data["json"] = Status{ErrCode: POST_ERR, Msg: "Error"}
			c.ServeJSON()
			c.StopRun()
		}
		c.Data["json"] = Status{ErrCode: POST_OK, Msg: "AddService success"}
		c.ServeJSON()
		c.StopRun()
	} else {
		c.Data["json"] = Status{ErrCode: POST_ERR, Msg: "Error"}
		c.ServeJSON()
		c.StopRun()
	}
}

// GetServiceInfoList 获取服务信息表
func (c *ServiceController) GetServiceInfoList() {
	c.IfHasSession()
	var from int64
	var to int64
	currentPage, _ := c.GetInt64("currentPage")
	pageSize, _ := c.GetInt64("pageSize")
	from = (currentPage - 1) * pageSize
	to = currentPage * pageSize
	list, i := models.GetAllServiceList(from, to)
	totalPage := i/pageSize + 1
	if i > 0 {
		c.Data["json"] = Status{Date: list, TotalPage: totalPage, ErrCode: GET_OK, Msg: "GetServiceInfoList:success"}
		c.ServeJSON()
		c.StopRun()
	} else if currentPage > totalPage {
		c.Data["json"] = Status{ErrCode: GET_ERR, Msg: "Error"}
		c.ServeJSON()
		c.StopRun()
	} else {
		c.Data["json"] = Status{ErrCode: GET_ERR, Msg: "Error"}
		c.Redirect("/page/err400", 302)
		c.ServeJSON()
	}
}

// ViewService 服务订单页面重定向
func (c *ServiceController) ViewService() {
	serviceID := c.GetString("serviceID")
	redirectURL := "/page/bill?serviceID=" + serviceID
	c.Data["json"] = map[string]string{"redirectUrl": redirectURL}
	c.ServeJSON()
}

// GetServiceInfo 获得服务详情
func (c *ServiceController) GetServiceInfo() {
	serviceID, _ := c.GetInt("serviceID")
	service, err := models.GetServiceViewByServiceID(serviceID)
	if err != nil {
		c.Data["json"] = Status{ErrCode: GET_ERR, Msg: "Error"}
		c.ServeJSON()
		c.StopRun()
	} else {
		c.Data["json"] = Status{Date: service, ErrCode: GET_OK, Msg: "GetServiceInfoList:success"}
		c.ServeJSON()
		c.StopRun()
	}

}
